const AJAX = getApp().ajax,
      $wx = getApp().$wx,
      getColor = getApp().globalData.getColor;

Page({
  data: {
    getColor: getColor(),
    isOpenColor: false,
    phone: null,
    vcTime: 60
  },

  onLoad(){
    $wx.setNavColor(getColor().val); 
  },

  onShareAppMessage(){

  },
  
  /*blur 获取手机号*/
  blurGetPhone(e){
    let td = this.data, phone = e.detail.value;

    if (phone.length > 0 && !$wx.checkPhone(phone)){
      $wx.showToast('请输入正确的手机号');
    }
  },

  /*input 获取手机号*/ 
  inputGetPhone(e){
    let td = this.data, phone = e.detail.value;  
      
    if (!$wx.checkPhone(phone)) {
      td.isOpenColor = false;
    } else {
      td.isOpenColor = true;
      this.setData({
        phone: phone
      });
    }

    this.setData({
      isOpenColor: td.isOpenColor
    });
  },
  
  /*获取验证码*/ 
  verificationCode(){
    let _this = this, td = _this.data;

    if (td.isOpenColor && td.vcTime == 60){
      AJAX.getVerificationCode({
        mobile: td.phone
      },(status,data,msg)=>{
        $wx.showToast(msg);
        if(status){
          let Interval = setInterval(callBack=>{
            if (td.vcTime == 1){
              clearInterval(Interval);
              _this.setData({
                vcTime: 60
              });
            }else{
              _this.setData({
                vcTime: --td.vcTime
              });
            }
          },1000);
        }
      });
    }
  },

  /*提交*/ 
  formSubmit(e){
    let form_obj = e.detail.value;

    if (form_obj.phone.length == 0){
      $wx.showToast('请输入手机号');
    } else if (form_obj.phone.length > 0 && !$wx.checkPhone(form_obj.phone)) {
      $wx.showToast('请输入正确的手机号');
    } else if (form_obj.vc.length == 0){
      $wx.showToast('请输入验证码');
    }else{
      wx.showLoading({
        title: '加载中...',
        mask: true
      });
      AJAX.updatePhone({
        mobile: form_obj.phone,
        code: form_obj.vc
      },(status, data, msg)=>{
        wx.hideLoading();
        $wx.showToast(msg);

        if(status){
          setTimeout(callBack =>{
            wx.navigateBack();
          }, 2000);
        }
      });
    }
  },

  /*获取验证码*/ 
});